package jaux.message.codec.utils;


public class BitUtils {

	/**
     * Convert bit array to int.
     *
     * @param bits the bits
     * @param signed the signed
     * @return the integer
     */
    public static int convertBitArrayToInt(int[] bits, boolean signed) {
        if (!signed) {
            return convertBitArrayToInt(bits);
        }
        int result = 0;
        int sign = bits[0] > 0 ? -1 : 1;
        int[] bits2 = new int[32];
        for (int i = 0; i < bits2.length - bits.length; i++) {
            bits2[i] = sign > 0 ? 0 : 1;
        }
        for (int i = bits2.length - bits.length; i < bits2.length; i++) {
            bits2[i] = bits[i + bits.length - bits2.length];
        }
        for (int i = 0; i < bits2.length; i++) {
            result += (bits2[i] & 0xff) << (bits2.length - i - 1);
        }
        return result;
    }
    
    /**
     * Convert bit array to int.
     *
     * @param bits the bits
     * @return the integer
     */
    public static int convertBitArrayToInt(int[] bits) {
        int result = 0;
        for (int i = 0; i < bits.length; i++) {
            result += (bits[i] & 0xff) << (bits.length - i - 1);
        }
        return result;
    }
}
